<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:bml="http://bookml.googlecode.com"
	xmlns:rng="http://relaxng.org/ns/structure/1.0" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
	xmlns:exsl="http://exslt.org/common">
	<xsl:param name="schema-name" select="BookML"/>
	<xsl:key name="define-key" match="//rng:define" use="@name"/>
	<xsl:variable name="all-defines" select="//rng:define"/>
	<xsl:template match="/">
		<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="rng:grammar">
		<bml:appendix xml:id="schema-reference" xmlns="http://bookml.googlecode.com">
			<bml:title>스키마 레퍼런스</bml:title>
			<bml:para>
				이곳에서는 <xsl:value-of select="$schema-name"
					/>의 XML 스키마에 따라 각 요소들의 상세한 구조에 대해 살펴보겠다.	따로 명시되지 않은 타입명은 <bml:link
					url="http://www.w3.org/TR/xmlschema-2/">XML 스키마의 기본타입</bml:link>을 따른다. 		
			</bml:para>
			<bml:note>
				<bml:title>자식요소의 중복도 표시</bml:title>
				<bml:para>
					자식요소의 이름 뒤에 다음과 같은 기호가 붙어 중복도를 표시한다.
				</bml:para>
				<bml:table cols="2" width="80%">
					<bml:colspec width="20%" talign="center" tvalign="center"/>
					<bml:colspec width="80%" tvalign="center"/>
					<bml:thead>
						<bml:row>
							<bml:cell>기호</bml:cell>
							<bml:cell>의미</bml:cell>
						</bml:row>
					</bml:thead>
					<bml:tbody>
						<bml:row>
							<bml:cell>?</bml:cell>
							<bml:cell>이 요소는 생략가능하다.</bml:cell>
						</bml:row>
						<bml:row>
							<bml:cell>+</bml:cell>
							<bml:cell>이 요소는 하나 이상 나온다.</bml:cell>
						</bml:row>
						<bml:row>
							<bml:cell>*</bml:cell>
							<bml:cell>이 요소는 생략되거나, 하나 이상 나온다.</bml:cell>
						</bml:row>
						<bml:row>
							<bml:cell>없음</bml:cell>
							<bml:cell>이 요소는 반드시 한 개 나와야 한다.</bml:cell>
						</bml:row>
					</bml:tbody>
				</bml:table>
			</bml:note>
			<bml:sect1>
				<bml:title>요소 설명</bml:title>
				<xsl:apply-templates select="//rng:element">
					<xsl:sort select="@name"/>
				</xsl:apply-templates>
			</bml:sect1>
			<bml:sect1>
				<bml:title>타입 설명</bml:title>
				<bml:para><xsl:value-of select="$schema-name"/>의 XML 스키마에서 정의하여 사용하는 공용 타입들은 다음과 같다.</bml:para>
				<xsl:call-template name="put-types"/>
			</bml:sect1>
		</bml:appendix>
	</xsl:template>

	<xsl:template name="put-types">
		<xsl:for-each select="$all-defines[ substring(@name, string-length(@name)-4, 5) = '-type' ]">
			<xsl:sort select="@name"/>
			<bml:sect2>
				<xsl:attribute name="xml:id">
					<xsl:value-of select="concat('scrf-type-', normalize-space(@name) )"/>
				</xsl:attribute>
				<bml:title>
					<xsl:value-of select="@name"/>
				</bml:title>
				<bml:para>
					<xsl:value-of select="a:documentation"/>
					<xsl:if test="not(substring(a:documentation, string-length(a:documentation), 1) = '.')">
						<xsl:text>.</xsl:text>
					</xsl:if>
				</bml:para>
				<xsl:choose>
					<xsl:when test="rng:choice">
						<xsl:call-template name="put-choice-values"/>
					</xsl:when>
				</xsl:choose>
			</bml:sect2>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="put-choice-values">
		<bml:table cols="2" width="80%">
			<xsl:attribute name="caption">
				<xsl:value-of select="concat( ancestor::*[1]/@name, '의 내용' )"/>
			</xsl:attribute>
			<bml:colspec width="20%" talign="center" tvalign="center"/>
			<bml:colspec width="50%" tvalign="center"/>
			<bml:thead>
				<bml:row>
					<bml:cell>내용값</bml:cell>
					<bml:cell>의미</bml:cell>
				</bml:row>
			</bml:thead>
			<bml:tbody>
				<xsl:choose>
					<xsl:when test="rng:choice/rng:value">
						<!-- assume all children is value type -->
						<xsl:for-each select="rng:choice/rng:value">
							<xsl:sort select="text()" data-type="number"/>
							<bml:row>
								<bml:cell>
									<xsl:value-of select="text()"/>
								</bml:cell>
								<bml:cell>
									<xsl:value-of select="following-sibling::a:documentation/text()"/>
								</bml:cell>
							</bml:row>
						</xsl:for-each>
					</xsl:when>
					<!--					<xsl:when test="rng:choice/rng:ref">
						<xsl:variable name="children">
							<xsl:call-template name="put-children"/>
						</xsl:variable>
						<xsl:for-each select="exsl:node-set( $children )/element">
							<xsl:sort select="@name"/>
							<bml:row>
								<bml:cell>
									<xsl:value-of select="@name"/>
								</bml:cell>
								<bml:cell>
									<xsl:value-of select="following-sibling::a:documentation/text()"/>
								</bml:cell>
							</bml:row>
						</xsl:for-each>
					</xsl:when>
-->
				</xsl:choose>
			</bml:tbody>
		</bml:table>
	</xsl:template>

	<xsl:template name="put-child">
		<xsl:param name="multiplicity" select="''"/>
		<xsl:variable name="lname" select="local-name()"/>
		<xsl:choose>
			<xsl:when test="$lname = 'element'">
				<element>
					<xsl:attribute name="name">
						<xsl:value-of select="@name"/>
					</xsl:attribute>
					<xsl:attribute name="multiplicity">
						<xsl:value-of select="$multiplicity"/>
					</xsl:attribute>
				</element>
			</xsl:when>
			<xsl:when test="$lname = 'optional'">
				<xsl:for-each select="rng:*">
					<xsl:call-template name="put-child">
						<xsl:with-param name="multiplicity" select="'?'"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$lname = 'zeroOrMore'">
				<xsl:for-each select="rng:*">
					<xsl:call-template name="put-child">
						<xsl:with-param name="multiplicity" select="'*'"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$lname = 'oneOrMore'">
				<xsl:for-each select="rng:*">
					<xsl:variable name="multi">
						<xsl:choose>
							<xsl:when test="$multiplicity = '?'">
								<xsl:text>*</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>+</xsl:text>
							</xsl:otherwise>
						</xsl:choose>						
					</xsl:variable>
					<xsl:call-template name="put-child">
						<xsl:with-param name="multiplicity" select="$multi"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$lname = 'choice'">
				<choice>
					<xsl:for-each select="rng:*">
						<xsl:call-template name="put-child">
							<xsl:with-param name="multiplicity" select="$multiplicity"/>
						</xsl:call-template>
					</xsl:for-each>
				</choice>
			</xsl:when>
			<xsl:when test="$lname = 'interleave'">
				<interleave>
					<xsl:for-each select="rng:*">
						<xsl:call-template name="put-child">
							<xsl:with-param name="multiplicity" select="$multiplicity"/>
						</xsl:call-template>
					</xsl:for-each>
				</interleave>
			</xsl:when>
			<xsl:when test="$lname = 'define'">
				<xsl:for-each select="rng:*">
					<xsl:call-template name="put-child">
						<xsl:with-param name="multiplicity" select="$multiplicity"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$lname = 'ref'">
				<xsl:variable name="rname" select="@name"/>
				<xsl:for-each select="key( 'define-key', $rname )">
					<xsl:call-template name="put-child">
						<xsl:with-param name="multiplicity" select="$multiplicity"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="put-children">
		<xsl:for-each select="rng:*">
			<xsl:call-template name="put-child"/>-
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="has-children">
		<xsl:param name="cname"/>
		<xsl:param name="depth" select="0"/>
		<xsl:variable name="lname" select="local-name()"/>
		<xsl:choose>
			<xsl:when test="$lname = 'element'">
				<xsl:choose>
					<xsl:when test="$cname = @name">
						<xsl:text>O</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:if test="$depth = 0">
							<xsl:for-each select="rng:*">
								<xsl:call-template name="has-children">
									<xsl:with-param name="cname" select="$cname"/>
									<xsl:with-param name="depth" select="1"/>
								</xsl:call-template>
							</xsl:for-each>
						</xsl:if>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when
				test="$lname = 'define' or $lname = 'optional' or $lname = 'zeroOrMore' or $lname = 'oneOrMore' or $lname = 'choice'">
				<xsl:for-each select="rng:*">
					<xsl:call-template name="has-children">
						<xsl:with-param name="cname" select="$cname"/>
						<xsl:with-param name="depth" select="$depth"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$lname = 'ref'">
				<xsl:variable name="rname" select="@name"/>
				<xsl:for-each select="key( 'define-key', $rname )">
					<xsl:call-template name="has-children">
						<xsl:with-param name="cname" select="$cname"/>
						<xsl:with-param name="depth" select="$depth"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="put-parents">
		<xsl:param name="nname"/>
		<xsl:for-each select="//rng:element[ not( @name = $nname ) ]">
			<xsl:variable name="hasp">
				<xsl:call-template name="has-children">
					<xsl:with-param name="cname" select="$nname"/>
					<xsl:with-param name="depth" select="0"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="string-length( $hasp ) > 0">
				<element>
					<xsl:attribute name="name">
						<xsl:value-of select="@name"/>
					</xsl:attribute>
				</element>
			</xsl:if>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="put-has-value">
		<xsl:for-each select="rng:*">
			<xsl:variable name="lname" select="local-name()"/>
			<xsl:choose>
				<xsl:when test="$lname = 'ref'">
					<xsl:variable name="name" select="@name"/>
					<xsl:for-each select="$all-defines[ @name = $name ]">
						<xsl:call-template name="put-has-value"/>
					</xsl:for-each>
				</xsl:when>
				<xsl:when test="$lname = 'value'">
					<xsl:text>t</xsl:text>
				</xsl:when>
				<xsl:when test="$lname = 'choice'">
					<xsl:call-template name="put-has-value"/>
				</xsl:when>
			</xsl:choose>
		</xsl:for-each>
	</xsl:template>

	<xsl:template match="rng:element">
		<xsl:variable name="ename" select="@name"/>
		<bml:sect2>
			<xsl:attribute name="xml:id">
				<xsl:value-of select="concat('scrf-', normalize-space($ename) )"/>
			</xsl:attribute>
			<bml:title>
				<xsl:value-of select="$ename"/>
			</bml:title>
			<xsl:variable name="doctext">
				<xsl:if test="a:documentation">
					<xsl:variable name="doc" select="normalize-space(a:documentation/text())"/>
					<xsl:value-of select="$doc"/>
					<xsl:if test="not(substring($doc, string-length($doc), 1) = '.')">
						<xsl:text>.</xsl:text>
					</xsl:if>
				</xsl:if>
			</xsl:variable>
			<xsl:variable name="has-value">
				<xsl:call-template name="put-has-value"/>
			</xsl:variable>
			<bml:para>
				<xsl:value-of select="$doctext"/>
				<xsl:text> </xsl:text>
				<xsl:choose>
					<xsl:when test="rng:data">
						<xsl:value-of select="rng:data/@type"/>
						<xsl:text> 형의 내용을 가진다.</xsl:text>
					</xsl:when>
					<xsl:when test="rng:ref[substring(@name, string-length(@name)-4, 5) = '-type']">
						<xsl:variable name="name" select="rng:ref[substring(@name, string-length(@name)-4, 5) = '-type']/@name"/>
						<bml:link refid="scrf-type-{$name}">
							<xsl:value-of select="$name"/>
						</bml:link>
						<xsl:text> 형의 내용을 가진다.</xsl:text>
					</xsl:when>
					<xsl:when test="rng:choice">
						<xsl:if test="string-length($has-value) > 0">
							<xsl:text>다음 값들 중 선택된 내용을 가진다.</xsl:text>
						</xsl:if>
					</xsl:when>
					<xsl:when test="rng:text">
						<xsl:text>문자열 형의 내용을 가진다.</xsl:text>
					</xsl:when>
				</xsl:choose>
			</bml:para>
			<xsl:if test="rng:choice and string-length($has-value) > 0">
				<xsl:call-template name="put-choice-values"/>
			</xsl:if>
			<xsl:variable name="attributes">
				<xsl:call-template name="put-attributes"/>
			</xsl:variable>
			<xsl:variable name="acnt" select="count( exsl:node-set( $attributes )/rng:attribute )"/>
			<xsl:choose>
				<xsl:when test="$acnt > 0">
					<xsl:choose>
						<xsl:when test="$acnt > 1">
							<bml:para>다음과 같은 속성들이 있다.</bml:para>
						</xsl:when>
						<xsl:otherwise>
							<bml:para>다음과 같은 속성이 있다.</bml:para>
						</xsl:otherwise>
					</xsl:choose>
					<bml:table cols="4">
						<xsl:attribute name="caption">
							<xsl:value-of select="concat( @name, '의 속성' )"/>
							<xsl:if test="count( exsl:node-set( $attributes )/rng:attribute ) > 1">
								<xsl:text>들</xsl:text>
							</xsl:if>
						</xsl:attribute>
						<bml:colspec width="20%" talign="center" tvalign="center"/>
						<bml:colspec width="20%" talign="center" tvalign="center"/>
						<bml:colspec width="6%" talign="center" tvalign="center"/>
						<bml:colspec width="54%"/>
						<bml:thead>
							<bml:row>
								<bml:cell>속성이름</bml:cell>
								<bml:cell>타입</bml:cell>
								<bml:cell>옵션</bml:cell>
								<bml:cell>설명</bml:cell>
							</bml:row>
						</bml:thead>
						<bml:tbody>
							<xsl:for-each select="exsl:node-set( $attributes )/rng:attribute">
								<xsl:sort select="@name"/>
								<bml:row>
									<bml:cell>
										<bml:code>
											<xsl:value-of select="@name"/>
										</bml:code>
									</bml:cell>
									<bml:cell>
										<xsl:call-template name="put-attrtype"/>
									</bml:cell>
									<bml:cell talign="center">
										<xsl:if test="@optional = 'true'">O</xsl:if>
									</bml:cell>
									<bml:cell>
										<xsl:value-of select="a:documentation"/>
									</bml:cell>
								</bml:row>
							</xsl:for-each>
						</bml:tbody>
					</bml:table>
				</xsl:when>
			</xsl:choose>
			<xsl:variable name="parents">
				<xsl:call-template name="put-parents">
					<xsl:with-param name="nname" select="$ename"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="pcnt" select="count( exsl:node-set( $parents )/element )"/>
			<xsl:if test="$pcnt > 0">
				<xsl:choose>
					<xsl:when test="$pcnt > 1">
						<bml:para>다음 부모요소들 아래에 나온다.</bml:para>
					</xsl:when>
					<xsl:otherwise>
						<bml:para>다음 부모요소 아래에 나온다.</bml:para>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:variable name="parent-nodes">
					<xsl:for-each select="exsl:node-set( $parents )/element">
						<xsl:sort select="@name"/>
						<xsl:copy>
							<xsl:copy-of select="@*"/>
						</xsl:copy>
					</xsl:for-each>
				</xsl:variable>
				<bml:para>
					<xsl:for-each select="exsl:node-set( $parent-nodes )/element">
						<bml:link refid="scrf-{normalize-space(@name)}">
							<xsl:value-of select="@name"/>
						</bml:link>
						<xsl:if test="following-sibling::element">, </xsl:if>						
					</xsl:for-each>
				</bml:para>
			</xsl:if>
			<xsl:variable name="children">
				<xsl:call-template name="put-children"/>
			</xsl:variable>
			<xsl:choose>
				<xsl:when test="exsl:node-set($children)/interleave/element">
					<xsl:variable name="ecnt" select="count( exsl:node-set( $children )/interleave/element )"/>
					<xsl:choose>
						<xsl:when test="$ecnt > 1">
							<bml:para>다음 자식요소들이 순서에 관계없이 나온다.</bml:para>
						</xsl:when>
						<xsl:otherwise>
							<bml:para>다음 자식요소가 나온다.</bml:para>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:call-template name="put-child-element">
						<xsl:with-param name="children" select="exsl:node-set($children)/interleave"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:when test="exsl:node-set($children)/choice/element">
					<xsl:variable name="ecnt" select="count( exsl:node-set( $children )/choice/element )"/>
					<xsl:choose>
						<xsl:when test="$ecnt > 1">
							<bml:para>다음 자식요소들 중 하나가 나온다.</bml:para>
						</xsl:when>
						<xsl:otherwise>
							<bml:para>다음 자식요소가 나온다.</bml:para>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:call-template name="put-child-element">
						<xsl:with-param name="children" select="exsl:node-set($children)/choice"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:when test="count( exsl:node-set( $children )/element ) > 0">
					<xsl:variable name="ecnt" select="count( exsl:node-set( $children )/element )"/>
					<xsl:choose>
						<xsl:when test="$ecnt > 1">
							<bml:para>다음 자식요소들이 순서대로 나온다.</bml:para>
						</xsl:when>
						<xsl:otherwise>
							<bml:para>다음 자식요소가 나온다.</bml:para>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:call-template name="put-child-element">
						<xsl:with-param name="children" select="$children"/>
					</xsl:call-template>
				</xsl:when>
			</xsl:choose>
		</bml:sect2>
	</xsl:template>
	<xsl:template name="put-child-element">
		<xsl:param name="children"/>
		<xsl:variable name="children-nodes">
			<xsl:for-each select="exsl:node-set( $children )/element">
<!--				<xsl:sort select="@name"/>-->
				<xsl:copy>
					<xsl:copy-of select="@*"/>
				</xsl:copy>
			</xsl:for-each>
		</xsl:variable>
		<bml:para>
			<xsl:for-each select="exsl:node-set( $children-nodes )/element">
				<bml:link refid="scrf-{normalize-space(@name)}">
					<xsl:value-of select="normalize-space(@name)"/>
				</bml:link>				
				<xsl:value-of select="@multiplicity"/>
				<xsl:if test="@optional = true()">
					<xsl:text>)</xsl:text>
				</xsl:if>
				<xsl:if test="following-sibling::element">
					<xsl:text>, </xsl:text>
				</xsl:if>
			</xsl:for-each>
		</bml:para>
	</xsl:template>

	<!--<xsl:template name="put-documentation">
		<xsl:choose>
			<xsl:when test="a:documentation">
				<xsl:value-of select="a:documentation"/>
			</xsl:when>
			<xsl:when test="rng:ref">
				<xsl:for-each select="rng:ref">
					<xsl:variable name="name" select="@name"/>
					<xsl:choose>
						<xsl:when test="following-sibling::a:documentation">
							<xsl:value-of select="following-sibling::a:documentation"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:for-each select="$all-defines[ @name = $name ]">
								<xsl:call-template name="put-documentation"/>
							</xsl:for-each>
						</xsl:otherwise>
					</xsl:choose>			
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise/>
		</xsl:choose>		
	</xsl:template>
-->
	<xsl:template name="put-attrtype">
		<xsl:choose>
			<xsl:when test="rng:data">
				<xsl:value-of select="rng:data/@type"/>
			</xsl:when>
			<xsl:when test="rng:value">
				<xsl:text>상수( </xsl:text>
				<xsl:value-of select="rng:value/text()"/>
				<xsl:text> )</xsl:text>
			</xsl:when>
			<xsl:when test="rng:ref">
				<xsl:if test="rng:ref[substring(@name, string-length(@name)-4, 5) = '-type']">
					<bml:para>
						<xsl:variable name="name" select="rng:ref[substring(@name, string-length(@name)-4, 5) = '-type']/@name"/>
						<bml:link refid="scrf-type-{$name}">
							<xsl:value-of select="$name"/>
						</bml:link>
						<xsl:text> 형</xsl:text>
					</bml:para>
				</xsl:if>
			</xsl:when>
			<xsl:when test="rng:text">text</xsl:when>
			<xsl:when test="rng:choice">
				<bml:para>
					<xsl:for-each select="rng:choice[1]/rng:value">
						<xsl:value-of select="text()"/>
						<xsl:if test="not(position() = last())">, </xsl:if>
					</xsl:for-each>
					<xsl:text> 중 선택</xsl:text>
				</bml:para>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="name()"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="put-attributes">
		<xsl:param name="optional" select="false()"/>
		<xsl:for-each select="rng:optional | rng:attribute | rng:ref">
			<xsl:call-template name="put-attribute">
				<xsl:with-param name="optional" select="$optional"/>
			</xsl:call-template>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="put-attribute">
		<xsl:param name="optional"/>
		<xsl:param name="documentation"/>
		<xsl:variable name="doc">
			<xsl:choose>
				<xsl:when test="string-length($documentation) > 0">
					<xsl:value-of select="$documentation"/>
				</xsl:when>
				<xsl:when test="following-sibling::*[1][ name() = 'a:documentation' ]">
					<xsl:value-of select="following-sibling::*[1][ name() = 'a:documentation' ]"/>
				</xsl:when>
			</xsl:choose>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="local-name() = 'optional'">
				<xsl:for-each select="*">
					<xsl:call-template name="put-attribute">
						<xsl:with-param name="optional" select="true()"/>
						<xsl:with-param name="documentation" select="$doc"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="local-name() = 'attribute'">
				<xsl:copy>
					<xsl:for-each select="@*">
						<xsl:copy-of select="."/>
					</xsl:for-each>
					<xsl:attribute name="optional">
						<xsl:value-of select="$optional"/>
					</xsl:attribute>
					<xsl:for-each select="*">
						<xsl:if test="not( name() = 'a:documentation' )">
							<xsl:copy-of select="."/>
						</xsl:if>
					</xsl:for-each>
					<a:documentation>
						<xsl:choose>
							<xsl:when test="string-length( $doc ) > 0">
								<xsl:value-of select="$doc"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="a:documentation"/>
							</xsl:otherwise>
						</xsl:choose>
					</a:documentation>
					<xsl:copy-of select="*"/>
				</xsl:copy>
			</xsl:when>
			<xsl:when test="local-name() = 'ref'">
				<xsl:variable name="name" select="@name"/>
				<xsl:for-each select="$all-defines[ @name = $name ]/*">
					<xsl:call-template name="put-attribute">
						<xsl:with-param name="optional" select="$optional"/>
						<xsl:with-param name="documentation" select="$doc"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="rng:*">
		<xsl:text>&#10;</xsl:text>
		<xsl:text>Unmatched element found : '</xsl:text>
		<xsl:value-of select="name(..)"/>
		<xsl:text>/</xsl:text>
		<xsl:value-of select="name()"/>
		<xsl:text>'</xsl:text>
	</xsl:template>

	<xsl:template match="text()">
		<xsl:value-of select="."/>
	</xsl:template>
</xsl:stylesheet>
